﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySQL_DataLayer;
using MySQL_Entities;
using System.Data.Services.Client;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace MySQL_WorkerRole
{
    public class WriteTableStorage
    {
        MySQLLogDAO logObj = new MySQLLogDAO(CloudStorageAccount.FromConfigurationSetting("DataConnectionString"));

        public void CreateTable()
        {
            try
            {
                //Create the table for the first time to store the logs
                var storage = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
                CloudTableClient tableClient = storage.CreateCloudTableClient();
                tableClient.CreateTableIfNotExist("MySQLLogs23Oct");

                //var table = TableStorage.Create(StorageAccountInfo.GetDefaultTableStorageAccountFromConfiguration());
                //table.TryCreateTable("MySQLLogs23Oct");
            }
            catch (Exception ex)
            {
            }
        }

        public void WriteToLog(string logtype, string description, string ipAddress)
        {
            try
            {
                MySQLLog logItem = new MySQLLog();
                logItem.LogType = "Information";
                logItem.Description = description;
                logItem.InstanceId = ipAddress;
                logItem.ModifiedOn = DateTime.UtcNow;
                logObj.Log(logItem);
            }
            catch (Exception ex)
            {
            }
        }
    }
}
